//
//  GIFTool.swift
//  Key2
//
//  Created by liujianlong on 2023/7/24.
//

import UIKit

class GIFTool: NSObject {
    class func find(_ block:((_ array:[GIFModel])->Void)?){
        GIFModel.bg_findAllAsync(nil){result  in
            DispatchQueue.main.async {
                block?(result as? [GIFModel] ?? [GIFModel]())
            }
        }
    }
    
    class func save(_ model:GIFModel?, _ block:((_ success:Bool)->Void)?){
        model?.bg_saveOrUpdateAsync{ result in
            DispatchQueue.main.async {
                block?(result)
            }
        }
    }
    class func delete(_ title:String,_ block:(()->Void)?){
        let condition = String(format:" %@ like '%%%@%%' ", bg_sqlKey("title"), title)
        let whereStr = String(format:"where%@", condition)
        print("文字 = %@   条件 = %@ ",title,whereStr)
        GIFModel.bg_deleteAsync(nil, where: whereStr){result in
            DispatchQueue.main.async {
                block?()
            }
        }
    }
    class func search(_ title:String,_ block:((_ array:[GIFModel])->Void)?){
        let condition = String(format:" %@ like '%%%@%%' ", bg_sqlKey("title"), title)
        let whereStr = String(format:"where%@", condition)
        print("文字 = %@   条件 = %@ ",title,whereStr)
        GIFModel.bg_findAsync(nil, where: whereStr){result  in
            DispatchQueue.main.async {
                block?(result as? [GIFModel] ?? [GIFModel]())
            }
        }
    }
}
